<style scoped lang="scss">
</style>
<template>
  <div class="app-container">
    <div class="">
      <el-form :inline="true" :model="listQuery">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="">
              <el-input v-model="listQueryFieldValue" placeholder="请输入内容" class="input-with-select" clearable>
                <el-select slot="prepend" v-model="listQueryField" placeholder="请选择" style="width: 120px">
                  <el-option v-for="(item,key) in listQueryFieldMap" :key="key" :value="item.field" :label="item.label">{{ item.label }}</el-option>
                </el-select>
              </el-input>
            </el-form-item>
            <el-form-item label="">
              <template slot="label">
                文章分类
              </template>
              <el-cascader ref="cascader" v-model="listQuery.category_id" :options="lessonCategoryTree" :props="{emitPath: false, value: 'id', label: 'name', checkStrictly: true}" filterable clearable style="width: 100%">
                <template v-slot="{ node, data }">
                  <span style="float: left;margin-right: 20px;">{{ data.name }}</span>
                </template>
              </el-cascader>
            </el-form-item>
            <el-form-item>
              <el-button v-waves plain class="filter-item" @click="resetGetList()">重置</el-button>
              <el-button v-waves type="primary" icon="el-icon-search" class="filter-item" @click="getList">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12" style="display: flex;flex-direction: row-reverse">
            <el-form-item>
              <el-button v-permission="['action:system:config:category:create']" v-waves type="primary" icon="el-icon-circle-plus-outline" class="filter-item" @click="clickCreate">新增文章</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
      border
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column align="center" type="index" width="70" label="序号" />
      <el-table-column label="文章分类" prop="category_info.name" />
      <el-table-column label="文章标题" prop="title" />
      <el-table-column label="文章简介" prop="summary" />
      <el-table-column label="创建用户" prop="created_admin.name" />
      <el-table-column label="创建时间" prop="created_at" />
      <el-table-column label="更新时间" prop="updated_at" />
      <el-table-column label="更新用户" prop="updated_admin.name" />
      <el-table-column fixed="right" label="操作" width="300">
        <template v-slot="scope">
          <el-button v-waves type="primary" size="mini" icon="el-icon-edit-outline" @click="clickUpdate(scope.row)">编辑</el-button>
          <el-button v-waves type="danger" size="mini" icon="el-icon-delete" @click="clickDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.page_size" @pagination="getList" />

    <!-- 新增|编辑弹窗 -->
    <el-dialog v-el-drag-dialog :close-on-click-modal="false" :title="formTitle" :visible.sync="formVisible" width="50%">
      <el-form ref="refForm" :model="form" :rules="rules" label-position="top" label-width="90px">
        <el-form-item label="" prop="parent_id">
          <template slot="label">
            文章分类
          </template>
          <el-cascader ref="cascader" v-model="form.category_id" :options="lessonCategoryTree" :props="{emitPath: false, value: 'id', label: 'name', checkStrictly: true}" filterable clearable style="width: 100%">
            <template v-slot="{ node, data }">
              <span style="float: left;margin-right: 20px;">{{ data.name }}</span>
            </template>
          </el-cascader>
        </el-form-item>
        <el-form-item label="文章标题" prop="title">
          <el-input v-model.trim="form.title" clearable />
        </el-form-item>
        <el-form-item label="文章简介" prop="summary">
          <el-input v-model.trim="form.summary" type="textarea" clearable />
        </el-form-item>
        <el-form-item label="中文内容" prop="zh_content">
          <el-input v-model.trim="form.zh_content" type="textarea" clearable />
        </el-form-item>
        <el-form-item label="英文内容" prop="en_content">
          <el-input v-model.trim="form.en_content" type="textarea" clearable />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button v-waves @click="formVisible = false">关闭</el-button>
        <el-button v-if="form.id === 0" v-waves type="primary" @click="submitCreate()">确认新增</el-button>
        <el-button v-else v-waves type="primary" @click="submitUpdate()">确认修改</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getLessonArticleList, lessonArticleCreate, lessonArticleUpdate, lessonArticleDelete, getLessonCategoryList } from '@/api/lesson/lesson'
import { treeFormatEmpty } from '@/utils'
export default {
  components: { Pagination },
  data() {
    return {
      lessonCategoryTree: [],
      list: null,
      total: 0,
      listLoading: false,
      listQueryFieldMap: [
        { field: 'title', label: '文章名称' }
      ],
      listQueryField: 'title',
      listQueryFieldValue: null,
      listQuery: {
        category_id: null,
        page: 1,
        page_size: 10,
        name: ''
      },
      formTitle: '新增',
      formVisible: false,
      form: {
        id: 0,
        category_id: 0,
        title: '',
        summary: '',
        zh_content: '',
        en_content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    listQueryField(newField, oldField) {
      this.listQuery[newField] = this.listQueryFieldValue
      this.listQuery[oldField] = null
    },
    listQueryFieldValue(newValue, oldValue) {
      this.listQuery[this.listQueryField] = newValue
    }
  },
  created() {
    this.resetGetList()
  },
  methods: {
    resetGetList() {
      this.listQuery = {
        page: 1,
        page_size: 10,
        keyword: ''
      }
      this.listQueryFieldValue = null
      this.getList()
      this.getLessonCategoryTree()
    },
    getLessonCategoryTree() {
      getLessonCategoryList().then(res => {
        this.lessonCategoryTree = treeFormatEmpty(res.data)
      })
    },
    getList() {
      this.listLoading = true
      getLessonArticleList(this.listQuery).then(res => {
        this.list = res.list
        this.total = res.total
      })
      this.listLoading = false
    },
    resetFormData() {
      this.form = {
        id: 0,
        name: '',
        remark: ''
      }
    },
    clickCreate() {
      this.resetFormData()
      this.formTitle = '新增'
      this.formVisible = true
    },
    submitCreate() {
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          lessonArticleCreate(this.form).then(res => {
            this.getList()
            this.$message({
              message: '创建成功',
              type: 'success'
            })
            this.formVisible = false
          }).then(res => {
          })
        }
      })
    },
    clickUpdate(item) {
      this.form = {
        id: item.id,
        category_id: item.category_id,
        title: item.title,
        summary: item.summary,
        zh_content: item.zh_content,
        en_content: item.en_content
      }
      this.formTitle = '编辑'
      this.formVisible = true
    },
    submitUpdate() {
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          lessonArticleUpdate(this.form).then(res => {
            this.getList()
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
            this.formVisible = false
          }).then(res => {
          })
        }
      })
    },
    clickDelete(item) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitDelete(item.id)
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submitDelete(id) {
      lessonArticleDelete({ id: id }).then(res => {
        this.getList()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.formVisible = false
      }).then(res => {
      })
    }
  }
}
</script>
